from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_tenant
import tenantEdit
#import res

class Tenant(QDialog, Ui_tenant.Ui_tenantDlg):
  def __init__(self, dbConn, dataGlob,  parent=None):
        super(Tenant, self).__init__(parent)
        self.setupUi(self)       
        self.initMenu()
        self.db = dbConn
        self.dataGlob = dataGlob
        self.mastModel = QSqlQueryModel(self)
        self.mastModel.setHeaderData(1, Qt.Horizontal, QVariant("Name"))
        self.mastModel.setHeaderData(2, Qt.Horizontal, QVariant("Address"))
        self.mastModel.setHeaderData(3, Qt.Horizontal, QVariant("City"))
        self.mastModel.setHeaderData(4, Qt.Horizontal, QVariant("PosCode"))
        self.mastModel.setHeaderData(5, Qt.Horizontal, QVariant("Phone"))
        self.mastModel.setHeaderData(6, Qt.Horizontal, QVariant("Mobile Ph"))
        self.mastModel.setHeaderData(7, Qt.Horizontal, QVariant("Fax"))
        self.mastModel.setHeaderData(8, Qt.Horizontal, QVariant("Tenant Type"))
        
        self.master_tv.setModel(self.mastModel)
        self.master_tv.setSelectionMode(QTableView.SingleSelection)
        self.master_tv.setSelectionBehavior(QTableView.SelectRows)        
        
  @pyqtSignature("")
  def on_name_le_editingFinished(self):
      if self.name_le.text() != "":
          self.name_cb.setCheckState(Qt.Checked)
      else:
          self.name_cb.setCheckState(Qt.Unchecked)
        
  @pyqtSignature("")
  def on_addr_le_editingFinished(self):
      if self.addr_le.text() != "":
          self.addr_cb.setCheckState(Qt.Checked)
      else:
          self.addr_cb.setCheckState(Qt.Unchecked)
                  
  def initMenu(self):
      self.retrieveAct = QAction(QIcon(":/retrieve.ico"), self.tr("Retrieve"), self)
#      self.retrieveAct.setShortcut ("Ctrl+R")
      self.connect(self.retrieveAct, SIGNAL("triggered()"), self.retrieveData)
      self.sortAct = QAction(QIcon(":/sort.ico"), self.tr("Sort"), self)
#      self.sortAct.setShortcut ("Ctrl+S")
      self.addAct = QAction(QIcon(":/add.ico"), self.tr("Add"), self)
#      self.addAct.setShortcut ("Ctrl+A")
      self.connect(self.addAct, SIGNAL("triggered()"), self.addData)
      self.editAct = QAction(QIcon(":/edit.ico"), self.tr("Edit"), self)
#      self.editAct.setShortcut ("Ctrl+E")
      self.connect(self.editAct, SIGNAL("triggered()"), self.editData)
      self.delAct = QAction(QIcon(":/del.ico"), self.tr("Delete"), self)
      self.connect(self.delAct, SIGNAL("triggered()"), self.delData)
#      self.delAct.setShortcut ("Ctrl+Del")
      self.printAct = QAction(QIcon("res/print.ico"), self.tr("Print Preview"), self)
#      self.printAct.setShortcut ("Ctrl+P")

  def retrieveData(self):
      qStr = "select p.kode_pemilik_penyewa, p.nama_pemilik_penyewa, p.alamat_pemilik_penyewa, p.kota_pemilik_penyewa, p.kode_pos, p.no_telp, p.no_ponsel, p.no_fax, p.kode_tt from master_pemilik_penyewa p left outer join master_type_tenant t on t.kode_tt = p.kode_tt"
      filt = ""
      urut = " order by p.nama_pemilik_penyewa "
      if self.name_cb.checkState() == Qt.Checked:
          filt = filt + " p.nama_pemilik_penyewa like '"+self.name_le.text()+"%' and "
      if self.addr_cb.checkState() == Qt.Checked:
          filt = filt + " p.alamat_pemilik_penyewa like '"+self.addr_le.text()+"%' and "
          
      if filt != "":
          filt = " where "+filt.left(len(filt)-4)
          
      qStr = qStr + filt + urut
      self.mastModel.setQuery(qStr, self.db)
      self.master_tv.setColumnHidden(0, True)
      self.master_tv.setColumnWidth(1, 200)
      self.master_tv.setColumnWidth(2, 300)
      self.master_tv.resizeRowsToContents()
      
  def addData(self):
      form = tenantEdit.TenantEdit(self.db, "add",  self.dataGlob, self)
      form.show()

  def editData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("kode_pemilik_penyewa").toString()
          form = tenantEdit.TenantEdit(self.db, id,  self.dataGlob, self)
          form.show()
          
  def delData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("p.kode_pemilik_penyewa").toString()
          nama = brs.value("p.nama_pemilik_penyewa").toString()
          if QMessageBox.question(self, "Tenant/Owner","Delete Data : "+nama+"?", QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
              qMast = QSqlQuery(self.db)
              self.db.transaction()
              qMast.exec_("delete from master_pemilik_penyewa where kode_pemilik_penyewa = '"+id+"'")
              self.db.commit()
              self.retrieveData()

  def sortData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def printData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def contextMenuEvent(self, event):
        menu = QMenu(self)
        menu.addAction(self.retrieveAct)
        menu.addAction(self.sortAct)
        menu.addSeparator()
        menu.addAction(self.addAct)
        menu.addAction(self.editAct)
        menu.addSeparator()
        menu.addAction(self.delAct)
        menu.addSeparator()
        menu.addAction(self.printAct)
        menu.exec_(event.globalPos())

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = Tenant()
    form.show()
    app.exec_()
